package com.niit.galgame.controller;

import com.niit.entity.Collect;
import com.niit.entity.Game;
import com.niit.entity.Upvote;
import com.niit.entity.User;
import com.niit.galgame.service.impl.GameServiceImpl;
import com.niit.galgame.service.impl.VoteServiceImpl;
import com.niit.utils.CommonResult;
import org.springframework.http.HttpCookie;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

@RestController
@RequestMapping("vote")
public class VoteController {
    @Resource
    private VoteServiceImpl voteService;

    @GetMapping("upvoteStatus")
    public CommonResult upvoteStatus(@PathParam("gid") int gid, HttpSession session){
        User user = (User) session.getAttribute("user");
        Upvote upvote = voteService.queryUpvote(gid,user.getUid());
        if(upvote == null)
            return new CommonResult(200,"未点赞");
        return new CommonResult(200,"已点赞");
    }

    @GetMapping("collectStatus")
    public CommonResult collectStatus(@PathParam("gid") int gid, HttpSession session){
        User user = (User) session.getAttribute("user");
        Collect collect = voteService.queryCollect(gid,user.getUid());
        if(collect == null)
            return new CommonResult(200,"未收藏");
        return new CommonResult(200,"已收藏");
    }

    @GetMapping("upvote")
    public CommonResult upvote(@PathParam("gid") int gid, HttpSession session){
        User user = (User) session.getAttribute("user");
        int status = voteService.upovteGame(gid,user.getUid());
        if(status == 1)
            return new CommonResult(200,"点赞成功");
        else
            return new CommonResult(200,"取消点赞");
    }

    @GetMapping("collect")
    public CommonResult collect(@PathParam("gid") int gid, HttpSession session){
        User user = (User) session.getAttribute("user");
        int status = voteService.collectGame(gid,user.getUid());
        if(status == 1)
            return new CommonResult(200,"收藏成功");
        else
            return new CommonResult(200,"取消收藏");
    }
}
